﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WeiboPlus.Models;
using WeiboPlus.Service;
using WeiboPlus.Common;

namespace WeiboPlus
{
    public class CompositeStatusService : IStatusService
    {
        IDictionary<SNSAccount, IStatusService> _svcs = new Dictionary<SNSAccount, IStatusService>();

        public bool IsActive
        {
            get
            {
                return _svcs.Keys.Any(key => key.IsActive);
            }
        }

        public void Register(SNSAccount account, IStatusService svc)
        {
            if (!_svcs.ContainsKey(account))
            {
                _svcs.Add(account, svc);
            }
        }

        public void Remove(SNSAccount account)
        {
            if (_svcs.ContainsKey(account))
            {
                _svcs.Remove(account);
            }
        }

        public bool Contains(SNSAccount account)
        {
            return _svcs.ContainsKey(account);
        }

  
        public void UpdateStatus(SNSRequest request, Action<SNSStatus, SNSResult> action)
        {
            if (!_svcs.Keys.Any(a => a.IsDestination))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.AtLeastOneAccountShouldBeDestination });
                }
                return;
            }

            foreach (var svc in _svcs)
            {
                if (svc.Key.IsDestination && !svc.Key.IsDuplicate)
                {
                    ThreadPool.QueueUserWorkItem((service) =>
                        {
                            var s = service as IStatusService;
                            if (s != null)
                                s.UpdateStatus(request, action);
                        }, svc.Value);
                }
            }

        }

        public void GetTimeline(SNSRequest request, Action<IList<SNSStatus>, SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount});
                }
            }
            ThreadPool.QueueUserWorkItem((o) =>
            {
                var rq = o as SNSRequest;
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate)
                        svc.Value.GetTimeline(rq, action);
                    Thread.Sleep(20);
                }
            }, request);
        }

        public void AddComment(SNSRequest request, Action<SNSStatus, SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void DeleteComment(SNSRequest request, Action<SNSStatus, SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void QueryCounts(SNSRequest request, Action<SNSCounts, SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount });
                }
            }

            ThreadPool.QueueUserWorkItem((o) =>
            {
                var rq = o as SNSRequest;
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate && svc.Key.ServiceProvider!= SNSProviderType.RenRen)
                        svc.Value.QueryCounts(rq, action);
                    Thread.Sleep(20);
                }
            }, request);
        }

        public void ResetCounts(SNSRequest request, Action<SNSCounts,SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount });
                }
            }

            ThreadPool.QueueUserWorkItem((o) =>
            {
                var rq = o as SNSRequest;
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate)
                        svc.Value.ResetCounts(rq, action);
                    Thread.Sleep(20);
                }
            }, request);
        }

        public void GetCounts(SNSRequest request, Action<IList<SNSCounts>, SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount });
                }
            }

            ThreadPool.QueueUserWorkItem((o) =>
            {
                var rq = o as SNSRequest;
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate && svc.Key.ServiceProvider== SNSProviderType.Sina)
                        svc.Value.GetCounts(rq, action);
                    Thread.Sleep(20);
                }
            },request);
        }

        public SNSAccount Account
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public SNSProvider Provider
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Func<SNSProvider> ProviderResolver
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IMappingFactory Factory
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Func<IMappingFactory> FactoryResolver
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        public void GetTrends(SNSRequest snsRequest, Action<SNSTrends, SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount });
                }
            }

            ThreadPool.QueueUserWorkItem((o) =>
            {
                var rq = o as SNSRequest;
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate)
                        svc.Value.GetTrends(rq, action);
                    Thread.Sleep(20);
                }
            }, snsRequest);
        }
    }
}
